package com.example.zhinongbao.presenter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import com.example.zhinongbao.Model.UserManager;
import com.example.zhinongbao.base.WeakRefHandler;
import com.example.zhinongbao.contract.MineContract;
import com.example.zhinongbao.entity.Goods;
import com.example.zhinongbao.entity.UserInfo;

import java.util.ArrayList;
import java.util.List;


public class MinePresenter implements MineContract.IMinePresenter {

    private static final int WHAT_GET_LOGIN_USER = 0;
    private static final int WHAT_LOGOUT_USER = 1;
    private MineContract.IMineView mMineView;
    private Context mContext;

    public MinePresenter(MineContract.IMineView mineView, Context context) {
        mMineView = mineView;
        mContext = context;
    }

    //    @Override
//    public void getLoginUser() {
//        new Thread(new Runnable() {
//            @SuppressLint("Range")
//            @Override
//            public void run() {
//                Uri uri = Uri.parse("content://com.example.zhinongbao.provider/user");
////                uri = uri.buildUpon().appendPath(id+"").build();
//                Log.d("uri", uri.toString());
//                Cursor cursor = mContext.getContentResolver().query(
//                        uri, null, null, null);
////                Log.d("cursor", "cursor.getCount() ===== "  + cursor.getCount());
//                try {
//                    if (cursor != null && cursor.moveToFirst()) {
//                        UserInfo userinfo = new UserInfo();
//                        userinfo.setUsername(cursor.getString(cursor.getColumnIndex("username")));
//                        userinfo.setUser_id(cursor.getInt(cursor.getColumnIndex("user_id")));
//                        userinfo.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
//                        Message message = Message.obtain();
//                        message.obj = userinfo;
//                        message.what = WHAT_GET_LOGIN_USER;
//                        mHandler.sendMessage(message);
//                    }
//                } finally {
//                    if (cursor != null) {
//                        cursor.close();
//                    }
//                }
//            }
//        }).start();
//    }
//
//    Handler mHandler = new WeakRefHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
//        @Override
//        public boolean handleMessage(@NonNull Message msg) {
//            int what = msg.what;
//            switch (what) {
//                case WHAT_GET_LOGIN_USER:
//                    mMineView.showUserInfo((UserInfo) msg.obj);
//                    break;
//            }
//            return false;
//        }
//    });
    @Override
    public void getLoginUser() {
        List<UserInfo> list = new ArrayList<>();
        new Thread(new Runnable() {
            @SuppressLint("Range")
            @Override
            public void run() {
                // 假设有一个方法 isLoggedIn() 来检查用户是否已经登录
                if (UserManager.isLoggedIn()) {
                    Uri uri = Uri.parse("content://com.example.zhinongbao.provider/userinfo");
                    Log.d("uri", uri.toString());
                    Cursor cursor = mContext.getContentResolver().query(
                            uri, null, null, null);
                    try {
                        if (cursor != null && cursor.moveToFirst()) {
                            do {
                                UserInfo userinfo = new UserInfo();
                                userinfo.setUsername(cursor.getString(cursor.getColumnIndex("username")));
                                userinfo.setUser_id(cursor.getString(cursor.getColumnIndex("user_id")));
                                userinfo.setPhone(cursor.getString(cursor.getColumnIndex("phone")));

                                Message message = Message.obtain();
                                message.obj = userinfo;
                                message.what = WHAT_GET_LOGIN_USER;
                                mHandler.sendMessage(message);
                                list.add(userinfo);
                            } while (cursor.moveToNext());
                        } else {
//                         如果没有找到用户信息，可能是用户未登录
                            UserInfo emptyUserInfo = new UserInfo(); // 空的用户信息对象
                            mMineView.showUserInfo(emptyUserInfo);
                        }
                    } finally {
                        if (cursor != null) {
                            cursor.close();
                        }
                    }
                } else {
                    mMineView.showUserInfo(null);
                }
            }
        }).start();
    }


    // 处理 Handler 消息
    Handler mHandler = new WeakRefHandler<MinePresenter>(Looper.getMainLooper(), this, new WeakRefHandler.Callback() {
        @Override
        public void handleMessage(Object associatedObject, Message msg) {
            int what = msg.what;
            switch (what) {
                case WHAT_GET_LOGIN_USER:
                    mMineView.showUserInfo((UserInfo) msg.obj);
                    break;
            }

        }

    });


}